//
//  MHTopicListRecommendTableViewCell.m
//  ManHua
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHTopicListRecommendTableViewCell.h"
#import "UIImage+Addiction.h"
#import "MHComicIntroductionCell.h"
#import "MHComicDetailViewController.h"

#import "NSString+Additional.h"
@interface MHTopicListRecommendTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UIButton *moreBookButton;
@property(nonatomic,assign)CGFloat cellheight;
@end
@implementation MHTopicListRecommendTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
          {
              self.selectionStyle = UITableViewCellSelectionStyleNone;
              _titleButton=[UIButton buttonWithType:UIButtonTypeSystem];
              _titleButton.frame=CGRectMake(10, 10, 20, 20);
              [_titleButton.layer setCornerRadius:10];
              CGRect rect1=_titleButton.frame;
//              _titleButton.titleLabel.textAlignment
              [_titleButton setTitle:@"✨"forState:UIControlStateNormal];
              [_titleButton setTitleColor:kThemeColor forState:UIControlStateNormal];
             
              [self.contentView addSubview:_titleButton];
             
              _titleName=[[UILabel alloc]initWithFrame:CGRectMake(rect1.origin.x+rect1.size.width, rect1.origin.y,Width/2 , rect1.origin.y+rect1.size.height)];
      
              _titleName.font=[UIFont fontWithName:@"Courier-Bold" size:14];
              _titleName.textColor=kThemeColor;
              CGRect rect2=_titleName.frame;
              [self.contentView addSubview:_titleName];
              _moreBookButton=[UIButton buttonWithType:UIButtonTypeSystem];
              _moreBookButton.frame=CGRectMake(Width-40, rect1.origin.y, 30,rect1.origin.y+rect1.size.height );
                           [_moreBookButton addTarget:self action:@selector(morebookPage:) forControlEvents:UIControlEventTouchUpInside];
        
              [_moreBookButton setImage:[[UIImage imageNamed:@"iconfont-iconfontgengduo"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
              [self.contentView addSubview:_moreBookButton];
              
              self.lineView=[[UIView alloc]init];
              self.lineView.frame=CGRectMake(0, CGRectGetMaxY(_moreBookButton.frame), Width, 0.5);
              self.lineView.backgroundColor=[UIColor lightGrayColor];
              [self.contentView addSubview:_lineView];
              UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
              flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
              flowLayout.minimumInteritemSpacing = 10;
              NSInteger row = 3;
              CGFloat pading=10;
              CGFloat ItemW=(Width-2*pading -(row-1)*pading)/3;
              CGFloat itemH = (ItemW - pading) * 11.0 / 9.0 + 35;
            self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(15, rect2.origin.y+rect2.size.height+10, Width-30, itemH + 20) collectionViewLayout:flowLayout];
  
            
              self.collectionView.backgroundColor=[UIColor whiteColor];
              self.collectionView.dataSource=self;
              self.collectionView.delegate=self;
              
              [self.collectionView registerNib:[UINib nibWithNibName:@"MHComicIntroductionCell" bundle:nil] forCellWithReuseIdentifier:@"MHComicIntroductionCell"];
              
              self.cellheight=CGRectGetMaxY( self.collectionView.frame);
           
              [self.contentView addSubview:self.collectionView];
           
   
          
}
    return self;

}
-(void)morebookPage:(UIButton*)button
{
    if (self.bookPageBlock!=nil) {
        self.bookPageBlock(self.mainTopicModel);
    }

}
-(void)setMainTopicModel:(MHMainTopicModel *)mainTopicModel
{
    _mainTopicModel=mainTopicModel;
    _titleName.text=mainTopicModel.Title;
   CGFloat width = [NSString textFrameWithFont:_titleName.font String:mainTopicModel.Title maxSize:CGSizeMake(0, _titleName.frame.size.height)].width;
    _titleName.frame=CGRectMake(_titleName.frame.origin.x, _titleName.frame.origin.y, width,_titleName.frame.size.height);
    
}

- (void)setBookArr:(NSArray *)bookArr
{
    _bookArr = bookArr;
    [self.collectionView reloadData];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.bookArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  MHComicIntroductionCell *showCell=[collectionView dequeueReusableCellWithReuseIdentifier:@"MHComicIntroductionCell" forIndexPath:indexPath];
    showCell.comicIntroduct= self.bookArr[indexPath.row];
    
    return showCell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    CGFloat margin=20;//
//    CGFloat padding=10;//左间隙
//    NSInteger row=3;
//    CGFloat itemW=(SCREEN_WIDTH-2*padding-(row-1)*padding)/3;
//    CGFloat itemH=(itemW-padding)*11.0/9.0+35;
//    return CGSizeMake(itemW, itemH);
    CGFloat pading=10;
    NSInteger row=3;
    
    CGFloat ItemW=(Width-2*pading -(row-1)*pading)/3;
    CGFloat ItemY=(ItemW-pading)*11.0/9+35;
    return CGSizeMake(ItemW, ItemY);
//    return CGSizeMake(105, collectionView.frame.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
-(CGFloat )heighOfCell
{
    return _cellheight+10;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ 
    
    if (self.comicDetailVCBlock !=nil)
{
    self.comicDetailVCBlock(self.bookArr[indexPath.row]);
}
    
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
